package task3.presto

import io.prestosql.jdbc.PrestoResultSet

import java.sql.{DriverManager, ResultSet}
import scala.util.Using

object PrestoCli {

  def main(args: Array[String]): Unit = {
    val url = "jdbc:presto://localhost:8080/tpcds/sf100"
    // try-with-resources
    Using.resource(DriverManager.getConnection(url, "zkx", null)) { con =>
      // HyperLogLog
      val hllRs = con.createStatement(
        ResultSet.TYPE_FORWARD_ONLY,
        ResultSet.CONCUR_READ_ONLY
      ).executeQuery("""select cardinality(approx_set(i_item_sk)) from tpcds.sf100.item""")
      println("HyperLogLog:")
      while (hllRs.next()) {
        println("_col0\n" + hllRs.getInt("_col0"))
      }

      // Count(Distinct)
      val cdRs = con.createStatement(
        ResultSet.TYPE_FORWARD_ONLY,
        ResultSet.CONCUR_READ_ONLY
      ).executeQuery("""select count(distinct(i_item_sk)) from tpcds.sf100.item""")
      println("Count(Distinct):")
      while (cdRs.next()) {
        println("_col0\n" + cdRs.getInt("_col0"))
      }
    }
  }
  
}
